<?php
/**
 * Created by PhpStorm.
 * User: lyang
 * Date: 2016/12/31
 * Time: 23:24
 */
function login_name_password($argv){
    function update_ip($id){
        $ip = $_SERVER["REMOTE_ADDR"];
        $DB = YC::make("DB");
        return $DB->update("yc_user",
            [
                "ip"=>$ip
            ]
            ,[
                "id[=]"=>$id
            ]);
    }
    if(isset($argv["name"]) && !empty($argv["name"]) && isset($argv["password"]) && !empty($argv["password"])){
            $DB = YC::make("DB");
            $data = $DB->get("yc_user","*",[
                "name[=]"=>addslashes($argv["name"])
            ]);
            if(isset($data["password"])){
                $password = YC::make("Crypt")->password($argv["password"]);
                if($password == $data["password"]){
                    $_SESSION["user_id"]=$data["id"];
                    $_SESSION["user_name"]=$data["name"];
                    update_ip($data["id"]);
                    YC::location(YC::get("ROOT")."/admin.php/default");
                }
            }
        return 0;
    }
    END:
    YC::location(YC::get("ROOT")."/index.php/login?msg=认证失败");
    return 1;

}
